package org.jeecg.modules.demo.zhibiao.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.zhibiao.DTO.ZhibiaoliebiaoDto;
import org.jeecg.modules.demo.zhibiao.entity.Zhibiaobiao;
import org.jeecg.modules.demo.zhibiao.service.IZhibiaobiaoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
 /**
 * @Description: 指标表
 * @Author: jeecg-boot
 * @Date:   2025-09-04
 * @Version: V1.0
 */
@Tag(name="指标表")
@RestController
@RequestMapping("/zhibiao/zhibiaobiao")
@Slf4j
public class ZhibiaobiaoController extends JeecgController<Zhibiaobiao, IZhibiaobiaoService> {
	@Autowired
	private IZhibiaobiaoService zhibiaobiaoService;
	
	/**
	 * 分页列表查询
	 *
	 * @param zhibiaobiao
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "指标表-分页列表查询")
	@Operation(summary="指标表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ZhibiaoliebiaoDto>> queryPageList(Zhibiaobiao zhibiaobiao,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									@RequestParam(name="mode", defaultValue = "all") String mode,
									HttpServletRequest req) {


        // 自定义查询规则
        Map<String, QueryRuleEnum> customeRuleMap = new HashMap<>();
        // 自定义多选的查询规则为：LIKE_WITH_OR
        customeRuleMap.put("zhibiaoleixing", QueryRuleEnum.LIKE_WITH_OR);
        customeRuleMap.put("zerenbumen", QueryRuleEnum.LIKE_WITH_OR);
		QueryWrapper<Zhibiaobiao> queryWrapper = QueryGenerator.initQueryWrapper(zhibiaobiao, req.getParameterMap(),customeRuleMap);

		Page<Zhibiaobiao> page = new Page<Zhibiaobiao>(pageNo, pageSize);
		IPage<Zhibiaobiao> pageList = zhibiaobiaoService.page(page, queryWrapper);
		IPage<ZhibiaoliebiaoDto> dtoPage = pageList.convert(entity -> {
			ZhibiaoliebiaoDto dto = new ZhibiaoliebiaoDto();
			BeanUtils.copyProperties(entity, dto);

			if ("filtered".equals(mode)) {
				LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
				String departId = sysUser.getOrgId();

				// 判断责任部门
				if (departId.equals(entity.getZerenbumen())) {
					dto.setZhuguanpeihe("我主管的");
				}
				// 判断配合部门
				else if (entity.getPeihebumen() != null && !entity.getPeihebumen().trim().isEmpty()) {
					// 分割并去除空格
					List<String> peiheDepartIds = Arrays.stream(entity.getPeihebumen().split(","))
							.map(String::trim)
							.collect(Collectors.toList());

					if (peiheDepartIds.contains(departId)) {
						dto.setZhuguanpeihe("我配合的");
					}
				}
			}

			return dto;
		});

		return Result.OK(dtoPage);

	}
	
	/**
	 *   添加
	 *
	 * @param zhibiaobiao
	 * @return
	 */
	@AutoLog(value = "指标表-添加")
	@Operation(summary="指标表-添加")
	@RequiresPermissions("zhibiao:x_zhibiaobiao:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Zhibiaobiao zhibiaobiao) {
		zhibiaobiaoService.addZhiBiao(zhibiaobiao);
		//zhibiaobiaoService.save(zhibiaobiao);

		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param zhibiaobiao
	 * @return
	 */
	@AutoLog(value = "指标表-编辑")
	@Operation(summary="指标表-编辑")
	@RequiresPermissions("zhibiao:x_zhibiaobiao:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Zhibiaobiao zhibiaobiao) {
		zhibiaobiaoService.updateById(zhibiaobiao);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "指标表-通过id删除")
	@Operation(summary="指标表-通过id删除")
	@RequiresPermissions("zhibiao:x_zhibiaobiao:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		zhibiaobiaoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "指标表-批量删除")
	@Operation(summary="指标表-批量删除")
	@RequiresPermissions("zhibiao:x_zhibiaobiao:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.zhibiaobiaoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "指标表-通过id查询")
	@Operation(summary="指标表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Zhibiaobiao> queryById(@RequestParam(name="id",required=true) String id) {
		Zhibiaobiao zhibiaobiao = zhibiaobiaoService.getById(id);
		if(zhibiaobiao==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(zhibiaobiao);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param zhibiaobiao
    */
    @RequiresPermissions("zhibiao:x_zhibiaobiao:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Zhibiaobiao zhibiaobiao) {
        return super.exportXls(request, zhibiaobiao, Zhibiaobiao.class, "指标表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("zhibiao:x_zhibiaobiao:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Zhibiaobiao.class);
    }

}
